/*
 ============================================================================
 Name        : GPIO_Main.c
 Author      : Stefan Nagel
 Version     : 0.0.1
 Copyright   : 2013 Stefan Nagel
 	 	 	 	This file is part of zeitmessanlage.
				zeitmessanlage is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
				zeitmessanlage is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
				You should have received a copy of the GNU General Public License along with zeitmessanlage. If not, see http://www.gnu.org/licenses/.
  Description : This is the driver for configuring the GNUBLIN GPIOs
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "GPIO_Main.h"


int GPIO_Init(int iGPIO, GPIODirectionType enDirection, GPIOEdgeType enEdge)
{
	char pcCommand[128];


	/* Setup the GPIO */
	sprintf(pcCommand					 ,"echo ");
	sprintf(pcCommand + strlen(pcCommand),"%d", iGPIO);
	sprintf(pcCommand + strlen(pcCommand)," > ");
	sprintf(pcCommand + strlen(pcCommand),"/sys/class/gpio/export");


	system(pcCommand);

	if(!GPIO_SetDirection(iGPIO,enDirection))
	{
		exit(-1);
	}
	if(!GPIO_SetEdge(iGPIO,enEdge))
	{
		exit(-1);
	}
	return(1);

}


int GPIO_SetEdge(int iGPIO, GPIOEdgeType enEdge)
{
	char pcCommand[128];

	sprintf(pcCommand					 ,"echo ");
	switch(enEdge)
	{
		case None:
			sprintf(pcCommand + strlen(pcCommand)," none ");
			break;
		case Falling:
			sprintf(pcCommand + strlen(pcCommand)," falling ");
			break;
		case Rising:
			sprintf(pcCommand + strlen(pcCommand)," rising ");
			break;
		case Both:
			sprintf(pcCommand + strlen(pcCommand)," both ");
			break;
		default:
			exit(-1);
	}

	sprintf(pcCommand + strlen(pcCommand)," > ");
	sprintf(pcCommand + strlen(pcCommand),"/sys/class/gpio/gpio");
	sprintf(pcCommand + strlen(pcCommand),"%d/", iGPIO);
	sprintf(pcCommand + strlen(pcCommand),"edge");

	if(system(pcCommand)<0)
	{
		printf("Error in execution of:\n %s \n", pcCommand);
		exit(-1);
	}

	return(1);
}


int GPIO_SetDirection(int iGPIO, GPIODirectionType enDirection)
{
	char pcCommand[128];

	sprintf(pcCommand					 ,"echo ");
	switch(enDirection)
	{
		case Input:
			sprintf(pcCommand + strlen(pcCommand)," in ");
			break;
		case Output:
			sprintf(pcCommand + strlen(pcCommand)," out ");
			break;
		default:
			exit(-1);
	}

	sprintf(pcCommand + strlen(pcCommand)," > ");
	sprintf(pcCommand + strlen(pcCommand),"/sys/class/gpio/gpio");
	sprintf(pcCommand + strlen(pcCommand),"%d/", iGPIO);
	sprintf(pcCommand + strlen(pcCommand),"direction");

	if(system(pcCommand)<0)
	{
		printf("Error in execution of:\n %s \n", pcCommand);
		exit(-1);
	}

	return(1);
}

int GPIO_Clean(int iGPIO)
{
	char pcCommand[128];

	sprintf(pcCommand					 ,"echo ");
	sprintf(pcCommand + strlen(pcCommand),"%d", iGPIO);
	sprintf(pcCommand + strlen(pcCommand)," > ");
	sprintf(pcCommand + strlen(pcCommand),"/sys/class/gpio/unexport");

	if(system(pcCommand)<0)
	{
		printf("Error in execution of:\n %s \n", pcCommand);
		exit(-1);
	}

	return(1);
}

